﻿using Microsoft.VisualStudio.Modeling;
using System;
using System.Collections.Generic;
using System.Text;

namespace Project.Co_DataTransformationPrototype
{
    public class DataTypeChangedMapping : MappingBase
    {
        
        public DataTypeChangedMapping(EventContext eventContext)
        {
            this.eventContext = eventContext;
            //generateSQLCodeMapping();
        }

        internal override void generateSQLCodeMapping(StringBuilder existingScript = null)
        {
            strSQLMapping = new StringBuilder();

            if (existingScript != null)
                strSQLMapping.Append(existingScript);

            setDataTypeChangedSQLCode();
        }

        private void setDataTypeChangedSQLCode()
        {
            Entity parentEntity;
            EntityAttribute attribute = eventContext.elementEventArgs.ModelElement as EntityAttribute;
            ElementPropertyChangedEventArgs eArgs = (ElementPropertyChangedEventArgs)eventContext.elementEventArgs;
            string strSQL = @"/*** --- Altering datatype for column '{0}' on table '{1}' --- ***/" + Environment.NewLine;

            if (attribute.IsDeleted)
                parentEntity = attribute.ParentEntity;
            else
                parentEntity = attribute.Entity;

            strSQLMapping.AppendFormat(strSQL, attribute.CurrentName, parentEntity.CurrentName);
            strSQLMapping.AppendLine(MappingTypeConvertion.getSQLStringForDataTypeConversion(parentEntity, attribute, (System.Data.SqlDbType)eArgs.OldValue, (System.Data.SqlDbType)eArgs.NewValue));
        }
    }
}
